/* @(#)GSServerThread.java   
 *
 * Project: GS-1
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-08-30 新增
 *   Janus Liu    2007-05-22 增加接口测试功能
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.integration.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.hitrust.gs.integration.GSApepInfoProcess;
import com.hitrust.gs.lib.GSLogWriter;

/**
 * 
 */
public class GSServerThread extends Thread {
    /**
     * 接口测试命令
     */
    public static final String TESTING_COMMAND = "TestJZCG";

    /**
     * 接口测试正常返回值
     */
    public static final String TESTING_OK = "TestOK";
    
    private static final Logger logger = Logger.getLogger("socket");
    
    private Socket         iClient;
    
    private DataInputStream in;

    private DataOutputStream    iOut;
    
    public GSServerThread(Socket s){
        this.iClient = s;
    }

    //  线程的执行方法
    public void run() {
        try {            
            String  iContent = ""; 
            GSLogWriter.debug(logger, this, "开始接收---");
            in = new DataInputStream(iClient.getInputStream());
            
            iOut = new DataOutputStream(iClient.getOutputStream());
        
            // 接收客户端传入内容
//            byte[] buffer = new byte[in.available()];
//            in.read(buffer);
//            String instr = new String(buffer, "GB2312");
            String instr = SocketUtil.receive(iClient, "GB2312", "");
			GSLogWriter.debug(logger, this, "client input is :" + instr);
			
            if ((instr != null) && (instr.indexOf(TESTING_COMMAND) == 0)) {
				GSLogWriter.debug(logger, this, "测试命令");
                SocketUtil.send(iClient, TESTING_OK, "utf-8", "");
            }
            else {
                while ((instr != null) && (instr.length() > 0)) { 
                    GSLogWriter.debug(logger, this, "client input is :" + instr);
                    iContent = iContent + instr;
                    int idx = iContent.toUpperCase().indexOf("&END");                
                    if (idx > -1){
                        String rt = "";
                        GSApepInfoProcess gsip = new GSApepInfoProcess();
                        rt = gsip.process(iContent.substring(0, idx ));  
    //                    iOut.write(rt.getBytes());
    //                    iOut.flush();
                        SocketUtil.send(iClient, rt, "utf-8", "");
                        break;
                    }
    //                instr = in.readLine();
                    instr = SocketUtil.receive(iClient, "GB2312", "");
                }
            }
        }
        catch (IOException e) {
//            e.printStackTrace();
            GSLogWriter.error(logger, this, e.toString());

        }catch(Exception e){
            GSLogWriter.error(logger, this, e.toString());
        }
        finally {
            //iOut.println(iResult);
            try {
                if (iOut != null) {
                    iOut.close();
                }
                if (in != null) {
                    in.close();
                }
                if (iClient != null) {
                    iClient.close();
                }
            }
            catch (IOException e) {
                GSLogWriter.error(logger, this, e.toString());
            }
        }
    }
}
